*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
**** Replication of Tables A1, A2, A3 and A4 of the Appendix ****

/*
0. Setting globals
1. Table A1
2. Table A2
3. Table A3
4. Table A4
5. Overall common support (mentioned in-text)

*/
******************************************************************

clear all
version 17.0
set more off
cap log close

/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Replication_data\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

/*--------------------------------
---------- 1. Table A1 -----------
----------------------------------*/

*** Geological characteristics of Africa as a whole 

insheet using "$Data/all_gen.csv", clear

collapse (sum) area, by(era)
drop if era==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

save "$Data/all_gen.dta", replace

*** Restricting to geological characteristics found in training area

insheet using "$Data/research_gen.csv", clear

collapse (sum) area, by(era)
drop if era==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

rename area area_r
merge 1:1 era using "$Data/all_gen.dta", nogen 

*** Outputting table 

order era area area_r 
gsort - area

label var area "All Africa"
label var area_r "Area with ASM data"
label var era "Era"

egen cs = total(area) if area_r!=. 
di cs 
drop cs

for var area*: replace X = round(X, 0.0001)

texsave using "$Tables/Table_cs_gen.tex", replace title("Bedrock ages present in African continent and areas with ASM data") varlabels location(h) frag


/*--------------------------------
---------- 2. Table A2 -----------
----------------------------------*/

*** Geological characteristics of Africa as a whole 

insheet using "$Data/all_maf.csv", clear

collapse (sum) area, by(agesrxtp)
drop if agesrxtp==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

save "$Data/all_maf.dta", replace

*** Restricting to geological characteristics found in training area

insheet using "$Data/research_maf.csv", clear

collapse (sum) area, by(agesrxtp)
drop if agesrxtp==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

rename area area_r
merge 1:1 agesrxtp using "$Data/all_maf.dta", nogen 

*** Outputting table

order agesrxtp area area_r 
gsort - area

label var area "All Africa"
label var area_r "Area with ASM data"
label var agesrxtp "Type of bedrock"

egen cs = total(area) if area_r!=. 
di cs 
drop cs

for var area*: replace X = round(X, 0.0000001)

texsave using "$Tables/Table_cs_maf.tex", replace title("Bedrock types present in African continent and areas with ASM data") varlabels location(h) frag

/*--------------------------------
---------- 3. Table A3 -----------
----------------------------------*/

*** Geological characteristics of Africa as a whole 

insheet using "$Data/all_glim.csv", clear

collapse (sum) area, by(litho)
drop if litho==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

save "$Data/all_glim.dta", replace

*** Restricting to geological characteristics found in training area

insheet using "$Data/research_glim.csv", clear

collapse (sum) area, by(litho)
drop if litho==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

*** Excluding NAM, ZAF, MOZ, MDG and SOM

rename area area_r
merge 1:1 litho using "$Data/all_glim.dta", nogen 

save "$Data/all_glim.dta", replace

insheet using "$Data/all_glim_excl.csv", clear

collapse (sum) area, by(litho)
drop if litho==""
egen areatotal = total(area)
replace area = area / areatotal
drop areatotal

rename area area_excl 
merge 1:1 litho using "$Data/all_glim.dta", nogen 

*** Outputting table

order litho area area_excl area_r 
gsort - area

label var area "All Africa"
label var area_excl "Africa excl. NAM, ZAF, MOZ, MDG, SOM"
label var area_r "Area with ASM data"
label var litho "Type of surface rock"

egen cs = total(area) if area_r!=. 
di cs 
drop cs

egen cs = total(area_excl) if area_r!=. 
di cs 
drop cs

drop if area<0.01

for var area*: replace X = round(X, 0.0001)

texsave using "$Tables/Table_cs_glim.tex", replace title("Surface rock types present in African continent and areas with ASM data") varlabels location(h) frag

/*--------------------------------
---------- 4. Table A4 -----------
----------------------------------*/

*** Geological characteristics of Africa as a whole 

insheet using "$Data/all_emag.csv", clear

keep _mean _stdev _min _max dn
save "$Data/all_emag.dta", replace

*** Geological characteristics of training area

insheet using "$Data/research_emag.csv", clear
keep _mean _stdev _min _max
rename (_mean _stdev _min _max) (_mean_r _stdev_r _min_r _max_r)
gen dn=1

merge 1:1 dn using "$Data/all_emag.dta", keepusing(_mean _stdev _min _max) nogen

drop dn

*** Outputting table

label var _mean "Mean: all Africa"
label var _stdev "SD: all Africa"
label var _mean_r "Mean: areas with ASM data"
label var _stdev_r "SD: areas with ASM data"
label var _min "Minimum: all Africa"
label var _max "Maximum: all Africa"
label var _min_r "Minimum: areas with ASM data"
label var _max_r "Maximum: areas with ASM data"

order _mean _stdev _min _max _mean_r _stdev_r _min_r _max_r

for var *: replace X = round(X, 0.0001)

texsave using "$Tables/Table_cs_emag.tex", replace title("Electromagnetism on African continent and areas with ASM data") varlabels location(h) frag

/*---------------------------------------------------------
--- 5. Overall common support (mentioned in-text) ---------
----------------------------------------------------------*/

insheet using "$Data/cs.csv", clear

*** Keep only areas with common support
keep if id_1=="3|2|1" | id_1=="4|3|2|1"
keep area

*** Calculating percentage 
replace area = area/29866652120283

*** Total area of common support is some of areas with common support
list
